package com.csair.irp.job.file.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.lang.reflect.Field;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * 反射过滤类属性工具类
 *
 * @author magic
 * @date 2017/8/31
 */
public class EtTranUtil {

    protected static final Logger logger = LoggerFactory.getLogger(EtTranUtil.class);

    /**
     * 把List数组里面的对象遍历过滤成正常格式的数值类型
     * @param list
     * @return
     */
    public static List<?> TansListDatas(List<?> list) {
        List resultDataList = new ArrayList();
        for (Object o : list) {
            resultDataList.add(TansListClassFieldTypes(o));
        }
        return resultDataList;
    }

    /**
     * 遍历对象的属性，避免造成格式转换异常
     *
     * @param o
     * @return
     */
    public static Object TansListClassFieldTypes(Object o) {
        if (o != null) {
            Class clazz = o.getClass();
            String genericType = "";
            Field[] fls = clazz.getDeclaredFields();
            for (Field f : fls) {
                f.setAccessible(true);
                genericType = f.getGenericType().toString();
                try {
                    if (f.get(o) == null || f.get(o).equals("")) {
                        f.set(o, TransJuageType(genericType));
                    }
                } catch (IllegalAccessException e) {
                    logger.info("类属性获取失败！");
                    e.printStackTrace();
                }
            }
        } else {
            return null;
        }
        return o;
    }

    /**
     * @param GenericType
     * @return String
     */
    public static String TransJuageType(String GenericType) {
        if (GenericType.equals("double") || GenericType.equals("class java.lang.Double")) {
            return "0.0";
        } else if (GenericType.equals("int") || GenericType.equals("class java.lang.Integer")) {
            return "0";
        } else if (GenericType.equals("long") || GenericType.equals("class java.lang.Long")) {
            return "0";
        } else if (GenericType.equals("float") || GenericType.equals("class java.lang.Float")) {
            return "0.0";
        }
        return "";
    }
    public static String  setDefaultvale(String s){
       if(s==null||s.equals("")){
           return "0";
       }
       return s;
    }


    /**
     * 解压缩某个压缩文件
     */
    public static void unRarFile(File rarFile, String targetPath) throws IOException {
        ZipFile zipFile=new ZipFile(rarFile.getAbsolutePath(), Charset.forName("GBK"));
        Enumeration files=zipFile.entries();
        while(files.hasMoreElements()){
            ZipEntry zipEntry= (ZipEntry) files.nextElement();
            if(!zipEntry.isDirectory()){
                String fileName=zipEntry.getName();
                File zfile=new File(targetPath+acqFileName(fileName));
                createFile(zfile,zipFile,zipEntry);
            }
        }
    }
    public static String acqFileName(String filePath){
        return filePath.substring(filePath.lastIndexOf("/")+1);
    }

    /**
     * 下载解压之后里面文件夹的文件
     * @param file
     * @param zipFile
     * @param zipEntry
     */
    public static  void createFile(File file, ZipFile zipFile, ZipEntry zipEntry) throws IOException {
        InputStream is = null;
        OutputStream os = null;
        try {
            is = zipFile.getInputStream(zipEntry);
            os = new FileOutputStream(file);
            byte[] bytes = new byte[1024];
            int len = 0;
            while ((len = is.read(bytes,0,bytes.length)) != -1) {
                os.write(bytes, 0, len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != is) {
                is.close();
            }
            if (null != os) {
                os.close();
            }
        }
    }

    /**
     * 移除字符串中的字母
     * @param str 字符串
     * @return
     */
    public static String removeStrLetter(String str){
        str = str.replaceAll("[a-zA-Z]","" );
        return str;
    }

}
